﻿using Dapper;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Caching.Memory;
using Volo.Abp.DependencyInjection;
using YAKEJI.Abp.Database;
using YAPACS.Workflows;

namespace YAPACS.RisServer.Workflows.ExamApply;

public class Register : WorkflowBase, IRegister, ITransientDependency
{
    private readonly IDatabaseConnectionProvider _connectionProvider;

    public Register(IDatabaseConnectionProvider connectionProvider)
    {
        _connectionProvider = connectionProvider;
    }

    protected override async Task<WorkflowOutput> InvokeCoreAsync(WorkflowInput input)
    {
        return new WorkflowOutput { Success = true };
        const string key = "ExamItemNumbers";
        if (input.ContainsKey(key) && input.GetValueOrDefault(key) is IList<string> examItemNumbers)
        {
            if (examItemNumbers.Count > 0)
            {
                await SetExamApplies([.. examItemNumbers]);
            }
            return new WorkflowOutput
            {
                Success = true
            };
        }
        return new WorkflowOutput
        {
            Success = false,
            Error = "未获取到检查申请数据"
        };
    }
    private async Task SetExamApplies(List<string> examApplyNumbers)
    {
        string dbFile = Path.Combine(@"c:\yapacs\mock-data", @"ExamApply.db");
        string connectionString = $@"Data Source={dbFile};";
        using var connection = new SqliteConnection(connectionString);
        SQLitePCL.Batteries.Init();
        string sql = @"update ExamApply set IsRegister=1 where ExamItemNumber in :examApplyNumbers";
        await connection.ExecuteAsync(sql, new { examApplyNumbers });
    }
    private async Task SetExamAppliesProc(List<string> examApplyNumbers)
    {
        const string connectionString = @"Server=192.168.2.82;Initial Catalog=HIS;User Id=sa;Password=pacs@1234;TrustServerCertificate=True";
        using var connection = _connectionProvider.GetDbConnection(connectionString, DatabaseType.SqlServer);
        await connection.ExecuteAsync("pacs_proc_exam_register", new { examApplyNumbers },commandType: System.Data.CommandType.StoredProcedure);
    }
}
